#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
// dijkstra
using namespace std;
using PII = pair<int, int>;
const int N = 1e5;

int n, m, fo, ed;
int h[N], e[N], ne[N], w[N], idx;
int dist[N];
bool state[N];

void add(int a, int b, int c){
    e[idx] = b;
    ne[idx] = h[a];
    w[idx] = c;
    h[a] = idx++;
}

int dijkstra(){
    memset(dist, 0x3f, sizeof dist);
    priority_queue<PII, vector<PII>, greater<PII>> que;
    dist[fo] = 0;
    que.push({0, fo});

    while(que.size()){
        PII temp = que.top();
        que.pop();

        int ver = temp.second, dis = temp.first;

        if(state[ver])continue;
        state[ver] = true;

        for (int u = h[ver]; u != -1; u = ne[u]){
            int k = e[u];

            if(dist[k] > dis + w[u]){
                dist[k] = dis + w[u];
                que.push({dist[k], k});
            }
        }
    }

    if(dist[ed] == 0x3f3f3f3f){
        return -1;
    }else{
        return dist[ed];
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    memset(h, -1, sizeof h);
    cin >> n >> m >> fo >> ed;


    for (int i = 0; i < m; i ++){
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
        add(b, a, c);
    }

    int k = dijkstra();

    cout << k << endl;
    return 0;
}